package com.xfashionbrand.ui.main;

import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Parcelable;
import android.support.v13.app.FragmentStatePagerAdapter;
import android.util.SparseArray;
import android.view.ViewGroup;

import com.xfashionbrand.ui.posts.PostsListFragment;

/**
 * pager adapter containing tab fragments used by WPMainActivity
 */
public class WPMainTabAdapter extends FragmentStatePagerAdapter {

    public static final int NUM_TABS = 9;
    public String[] titles = {"新闻", "男装", "女装", "休闲", "运动", "鞋业", "羽绒服", "羊绒", "皮草"};

    static final int TAB_MY_SITE = 0;
    static final int TAB_READER  = 1;
    static final int TAB_ME      = 2;
    static final int TAB_NOTIFS  = 3;

    private final SparseArray<Fragment> mFragments = new SparseArray<>(NUM_TABS);

    public WPMainTabAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public void restoreState(Parcelable state, ClassLoader loader) {
        // work around "Fragement no longer exists for key" Android bug
        // by catching the IllegalStateException
        // https://code.google.com/p/android/issues/detail?id=42601
        try {
            super.restoreState(state, loader);
        } catch (IllegalStateException e) {
            // nop
        }
    }

    @Override
    public int getCount() {
        return NUM_TABS;
    }

    public boolean isValidPosition(int position) {
        return (position >= 0 && position < getCount());
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return PostsListFragment.newInstance(false, "post");
            case 1:
                return PostsListFragment.newInstance(true, "product_man");
            case 2:
                return PostsListFragment.newInstance(true, "product_woman");
            case 3:
                return PostsListFragment.newInstance(true, "product_casual");
            case 4:
                return PostsListFragment.newInstance(true, "product_sports");
            case 5:
                return PostsListFragment.newInstance(true, "product_shoes");
            case 6:
                return PostsListFragment.newInstance(true, "product_jacket");
            case 7:
                return PostsListFragment.newInstance(true, "product_cashmere");
            case 8:
                return PostsListFragment.newInstance(true, "product_fur");
            default:
                return null;
        }
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return titles[position];
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Object item = super.instantiateItem(container, position);
        if (item instanceof Fragment) {
            mFragments.put(position, (Fragment) item);
        }
        return item;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        mFragments.remove(position);
        super.destroyItem(container, position, object);
    }

    public Fragment getFragment(int position) {
        if (isValidPosition(position)) {
            return mFragments.get(position);
        } else {
            return null;
        }
    }

}
